package utils

import (
	"encoding/json"
	"github.com/gogo/protobuf/proto"
	"longmen/server/internal/rpc/tencentim/imsdk"
	"longmen/server/pkg/common/lib/bytesconv"
	pb "longmen/server/pkg/protobuf"
	"reflect"
	"strings"
)

func callbackResp(code int, status, ext string, data interface{}, message string) *pb.CallbackResp {
	bytes, _ := json.Marshal(data)
	result := &pb.CallbackResp{}
	result.MsgBody = append(result.MsgBody, &pb.CallbackResp_Msgbody{
		MsgType: imsdk.TIM_MSG_TYPE_CUSTOM,
		MsgContent: &pb.CallbackResp_Msgcontent{
			Data: bytesconv.BytesToString(bytes),
			Ext:  ext,
		},
	})
	result.ErrorCode = uint32(code)
	result.ActionStatus = status
	result.ErrorInfo = message
	return result
}

func CallbackFailResp(data interface{}, message string) *pb.CallbackResp {
	return callbackResp(imsdk.TIM_ERROR_ERROR, imsdk.OKActionStatus, "", data, message)
}

// 传入的参数会变成消息
func CallbackOkResp(data interface{}) *pb.CallbackResp {
	if data == nil {
		return CallbackNoChangeDataResp(imsdk.OKActionStatus, "", imsdk.TIM_CODE_OK)
	}
	return callbackResp(imsdk.TIM_CODE_OK, imsdk.OKActionStatus, "", data, "")
}

func CallbackRespWithCode(code int, sMsg string, data interface{}) *pb.CallbackResp {
	return callbackResp(code, imsdk.OKActionStatus, "", data, sMsg)
}

func CallbackOkAndExt(data interface{}, ext string) *pb.CallbackResp {
	return callbackResp(imsdk.TIM_CODE_OK, imsdk.OKActionStatus, ext, data, "")
}

func CallbackNoSendResp() (resp *pb.CallbackResp) {
	resp.ActionStatus = imsdk.OKActionStatus
	resp.ErrorCode = imsdk.TIM_ERROR_CODE_IGNORE
	resp.ErrorInfo = ""
	return resp
}

func CallbackNoChangeDataResp(status, message string, code int) *pb.CallbackResp {
	result := &pb.CallbackResp{}

	result.ErrorCode = uint32(code)
	result.ActionStatus = status
	result.ErrorInfo = message
	return result
}

func callbackRespText(code int, status, data, message, ext string) *pb.CallbackResp {
	result := &pb.CallbackResp{}
	result.MsgBody = append(result.MsgBody, &pb.CallbackResp_Msgbody{
		MsgType: imsdk.TIM_MSG_TYPE_TEXT,
		MsgContent: &pb.CallbackResp_Msgcontent{
			Text: data,
		},
	})
	result.ErrorCode = uint32(code)
	result.ActionStatus = status
	result.ErrorInfo = message
	result.CloudCustomData = ext
	return result
}

// OkRespText 0 data 1 ext
func OkRespText(data string, args ...string) *pb.CallbackResp {
	return callbackRespText(imsdk.TIM_CODE_OK, imsdk.OKActionStatus, data, "", args[0])
}

func FailRespText(error string) *pb.CallbackResp {
	return callbackRespText(imsdk.TIM_ERROR_ERROR, imsdk.FailActionStatus, "", error, "")
}

func FailCodeResp(code int, error string) *pb.CallbackResp {
	return callbackRespText(code, imsdk.FailActionStatus, "", error, "")
}

func C2CRejectMessage(error string) *pb.CallbackResp {
	return callbackRespText(imsdk.TIM_C2C_REJECT, imsdk.FailActionStatus, "", error, "")
}

func EsTermsTransSlice(item interface{}) []interface{} {
	t := reflect.TypeOf(item)
	v := reflect.ValueOf(item)
	if t.Kind() != reflect.Slice {
		return []interface{}{}
	}
	var ret []interface{}
	for i := 0; i < v.Len(); i++ {
		ret = append(ret, v.Index(i).Interface())
	}
	return ret
}

func PlatformTrans(optPlatForm string) int {
	switch strings.ToLower(optPlatForm) {
	case "ios":
		return 1
	case "android":
		return 2
	default:
		return 0 //RESTAPI Web Windows、Mac、iPad、Unknown
	}
}

func ToProtoData(message proto.Message) ([]byte, error) {
	return proto.Marshal(message)
}
